﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using Microsoft.Xna.Framework.Graphics;
using Somnus.Core;
using Microsoft.Xna.Framework;

namespace Somnus.Objects.GameObjects.LevelObjects
{
    class Spring : PhysicsObject
    {
        public float springForce;

        public Spring(DreamWorld world, String name, Texture2D texture, Nullable<Rectangle> sourceRectangle, BodyType bodyType, float x, float y, float width, float height)
            : base(world, name, texture, sourceRectangle, bodyType, x, y, width, height)
        {
            springForce = 13;
        }

        protected override bool HandleBeginContact(Fixture thisFixture, Fixture otherFixture, Contact contact)
        {
            if (otherFixture.UserData is Character)
            {
                //Divide a velocidade atual por três e aplica uma velocidade nova baseada na angulação da mola
                Vector2 velocity = (otherFixture.UserData as PhysicsObject).body.LinearVelocity;
                velocity.X = velocity.X / 3 + (float)(Math.Sin((double)(MathHelper.ToRadians(this.Rotation))) * springForce);
                velocity.Y = velocity.Y / 3 + -(float)(Math.Cos((double)(MathHelper.ToRadians(this.Rotation))) * springForce);
                (otherFixture.UserData as PhysicsObject).body.LinearVelocity = velocity;
                
           
                return true;
            }
            else
            {
                return false;
            }
            
        }
    }
}
